package com.cne.gmail.Gmail.controller;

import com.cne.gmail.Gmail.dao.GroupRepository;
import com.cne.gmail.Gmail.model.CneGroup;
import com.cne.gmail.Gmail.response.GmailServiceResponse;
import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 分组接口
 */
@Log4j2
@Api(description ="分组接口")
@RestController
@RequestMapping("group")
public class CneGroupController {
    @Autowired
    GroupRepository groupRepository;
    @PostMapping("insert")
    @ApiOperation(value = "增加分组信息",notes = "增加分组信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "groupName",value = "分组名",required = true,dataType = "String"),
            @ApiImplicitParam(paramType = "query",name = "me",value = "用户",required = true,dataType = "String")
    })
    public GmailServiceResponse insert(@RequestParam String me,String groupName){
        GmailServiceResponse response=new GmailServiceResponse();
        CneGroup result = groupRepository.findByMe(me);
        try {
            if (result==null){
                CneGroup group=new CneGroup();
                List list=Lists.newArrayList();
                list.add(groupName);
                group.setMe(me);
                group.setGroupName(list);
                CneGroup save = groupRepository.save(group);
                response.setMsg("添加成功");
                response.setCode(200);
                response.setData(save);
                return response;
            }else if (result.getGroupName().contains(groupName)){
                response.setMsg("该分组已存在，请检查后重新添加");
                return  response;
            }else {
                result.getGroupName().add(groupName);
                CneGroup save = groupRepository.save(result);
                response.setMsg("添加成功");
                response.setCode(200);
                response.setData(save);
                return response;
            }
        }catch (Exception e){
            response.setMsg("添加失败");
            log.info(e.getMessage());
            return response;
        }
    }
    @DeleteMapping("delete")
    @ApiOperation(value = "删除分组信息",notes = "删除分组信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "groupName",value = "分组名",required = true,dataType = "String")
    })
    public GmailServiceResponse delete(@RequestParam String me,@RequestParam String groupName){
        GmailServiceResponse response=new GmailServiceResponse();
        CneGroup result = groupRepository.findByMe(me);
        try {
            if (result!=null){
                result.getGroupName().remove(groupName);
                groupRepository.save(result);
                response.setMsg("删除成功");
                return  response;
            }else {
                response.setMsg("未找到该分组");
                return response;
            }
        }catch (Exception e){
            response.setMsg("删除失败");
            log.info(e.getMessage());
            return response;
        }
    }
    @GetMapping("search")
    @ApiOperation(value = "查询分组信息",notes = "增加分组信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "me",value = "用户邮箱名",required = true,dataType = "String")
    })
    public GmailServiceResponse search(@RequestParam String me){
        GmailServiceResponse response=new GmailServiceResponse();
        CneGroup result = groupRepository.findByMe(me);
        if (result!=null){
            response.setData(result.getGroupName());
            response.setCode(200);
            response.setCode(200);
            return response;
        }else {
            response.setMsg("该用户暂无分组信息");
            return response;
        }
     }

   }
